<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

if (isset($_GET["filebrowser"])){
	$type = $_GET["type"];
	$site_dir = $_GET["site_dir"];

	if ($type == "file"){
		include("file-browser.php");
		$fdir = "$site_dir/user_files/files/";
		$imb_head = get_filebrowser("thediv", "", "../$fdir", "../");

	} elseif ( $type == "image" ){
		include("image-browser.php");

		$fdir = "$site_dir/user_files/images/";

		$imb_head = get_imagebrowser("thediv", "", "$fdir", "../");
	}
    $html = "<html>
    <head>
    <script type='text/javascript' src='../ext_lib/tiny_mce/tiny_mce_popup.js'></script>
    <script>
    var FileBrowserDialogue = {
		init : function () {
			// Here goes your code for setting your custom things onLoad.
		},
		mySubmit : function () {
			// Here goes your code to insert the retrieved URL value into the original dialogue window.
			// For example code see below.
		}
	}

	tinyMCEPopup.onInit.add(FileBrowserDialogue.init, FileBrowserDialogue);

	var win = tinyMCEPopup.getWindowArg('window');
	var input = tinyMCEPopup.getWindowArg('input');
	var res = tinyMCEPopup.getWindowArg('resizable');
	var inline = tinyMCEPopup.getWindowArg('inline');

    function cb2(im){
        var im = '$fdir' + im;
        win.document.getElementById(tinyMCEPopup.getWindowArg('input')).value = im;
        tinyMCEPopup.close();

    }

    </script>
<br/><br/>
    $imb_head

    </head>
    <body onload='show_${type}browser(cb2)'>
    <div id='thediv' style='width: 100%; height:100%'></div>
    </body>
    </html>";
    echo $html;

} else {
$editor = '

<script type="text/javascript" src="ext_lib/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">

var r = 0;
var im = "monkey";
function return_image(i){
	im = im;
	r = 1;
}

function fileBrowser(field_name, url, type, win) {

    // alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing

    /* If you work with sessions in PHP and your client doesnt accept cookies you might need to carry
       the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
       These lines of code extract the necessary parameters and add them back to the filebrowser URL again. */

    var cmsURL = "admin/edit-tinymce.php?filebrowser=1&site_dir='.$site_dir.'";    // script URL - use an absolute path!
    if (cmsURL.indexOf("?") < 0) {
        //add the type as the only query parameter
        cmsURL = cmsURL + "?type=" + type;
    }
    else {
        //add the type as an additional query parameter
        // (PHP session ID is now included if there is one at all)
        cmsURL = cmsURL + "&type=" + type;
    }

    tinyMCE.activeEditor.windowManager.open({
        file : cmsURL,
        title : "My File Browser",
        width : 420,  // Your dimensions may differ - toy around with them!
        height : 400,
        resizable : "yes",
        inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous : "no"
    }, {
        window : win,
        input : field_name
    });
    return false;
  }

tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	file_browser_callback : "fileBrowser",
	plugins : "spellchecker,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	// Theme options
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	extended_valid_elements : "form[action|name|method],iframe[src|frameborder|height|width]",
});

</script>

';
}
